const BaseController = require('./baseController')
const qiniu = require('qiniu')

class UploadController extends BaseController {
  get modelName () {
    return 'Upload'
  }

  async uploadAction () {
    const { ctx } = this
    let { ak, sk, bucket } = this.config.qiniu
    let mac = new qiniu.auth.digest.Mac(ak, sk)
    let options = {
      scope: bucket
    }
    let putPolicy = new qiniu.rs.PutPolicy(options)
    let uploadToken = putPolicy.uploadToken(mac)
    // 给前端返回一个uploadToken
    let body = {
      uptoken: uploadToken
    }
    ctx.body = body
    return body
  }
}

module.exports = UploadController
